package com.huilan.hgt.exception;

import com.huilan.hgt.util.Constants;
import com.huilan.hgt.util.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author ghy
 * @date 2018-06-07 15:42:17
 * @description 全局异常信息处理
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    @ResponseStatus(value = HttpStatus.OK)
    public final ResponseResult handleException(HttpServletRequest request, BusinessException ex) {
        return new ResponseResult(ex.getExceptionEnum());
    }


    /**
     * 捕获  RuntimeException 异常
     * @param request  request
     * @param e        exception
     * @param response response
     * @return 响应结果
     */
    @ExceptionHandler(RuntimeException.class)
    public ResponseResult runtimeExceptionHandler(HttpServletRequest request, final Exception e, HttpServletResponse response) {
        response.setStatus(HttpStatus.BAD_REQUEST.value());
        e.printStackTrace();
        return  ResponseResult.error(Constants.HTTP_STATUS.CODE_500, e.toString());
    }

    /**
     * 通用的接口映射异常处理方
     */
    @Override
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers,
                                                             HttpStatus status, WebRequest request) {

        if (ex instanceof MethodArgumentNotValidException ) {
            MethodArgumentNotValidException exception = (MethodArgumentNotValidException) ex;
            String message = exception.getBindingResult().getAllErrors().get(0).getDefaultMessage();
            log.error("非法参数:{}",message);
            return new ResponseEntity<>(ResponseResult.error(status.value(), message), status);
        }
        if (ex instanceof MethodArgumentTypeMismatchException) {
            MethodArgumentTypeMismatchException exception = (MethodArgumentTypeMismatchException) ex;
            String message = exception.toString();
            log.error(message);
            return new ResponseEntity<>(ResponseResult.error(status.value(), message), status);
        }

        ex.printStackTrace();

        if(ex instanceof NoHandlerFoundException){
            return new ResponseEntity<>(ResponseResult.error(status.value(), ex.toString()), status);
        }
        return new ResponseEntity<>(ResponseResult.error(status.value(), ex.toString()), status);
    }

}
